@page
@model ModelBindingSample.Pages.InstructorsWithCollection.CreateModel

@{
    ViewData["Title"] = "Create";
}
@{
    int format = 0;
}

<h2>Create</h2>

<h4>Instructor with Courses collection</h4>
<hr />
<div class="row">
    <div class="col-md-4">
        <form method="post">
            <div asp-validation-summary="All" class="text-danger"></div>
            <div class="form-group">
                <label asp-for="Instructor.LastName" class="control-label"></label>
                <input asp-for="Instructor.LastName" class="form-control" />
                <span asp-validation-for="Instructor.LastName" class="text-danger"></span>
            </div>
            <div class="form-group">
                <label asp-for="Instructor.FirstMidName" class="control-label"></label>
                <input asp-for="Instructor.FirstMidName" class="form-control" />
                <span asp-validation-for="Instructor.FirstMidName" class="text-danger"></span>
            </div>
            <div class="form-group">
                <label asp-for="Instructor.HireDate" class="control-label"></label>
                <input asp-for="Instructor.HireDate" class="form-control" value="" />
                <span asp-validation-for="Instructor.HireDate" class="text-danger"></span>
            </div>
            <div class="form-group">
                <div class="col-md-offset-2 col-md-10">
                    <table>
                        <tr>
                            @{
                                int cnt = 0;

                                foreach (var course in Model.AssignedCourseDataList)
                                {
                                    if (cnt % 3 == 0)
                                    {
                                    @:</tr><tr>
                                    }
                                    @:<td>
                                        format = 1;
                                        //format 1: "selectedCourses=1050&selectedCourses=2000
                                        //format 2: "selectedCourses[0]=1050&selectedCourses[1]=2000
                                        //format 3: "selectedCourses[]=1050&selectedCourses[]=2000
                                        if (format == 1)
                                        {
                                            <input type="checkbox"
                                                   name="selectedCourses"
                                                   value="@course.CourseID"
                                                   @(Html.Raw(course.Assigned ? "checked=\"checked\"" : "")) />
                                            @course.CourseID @:  @course.Title
                                        }
                                        else if (format == 2)
                                        {
                                            <input type="checkbox"
                                                   name="selectedCourses[@cnt]"
                                                   value="@course.CourseID"
                                                   @(Html.Raw(course.Assigned ? "checked=\"checked\"" : "")) />
                                            @course.CourseID @:  @course.Title
                                        }
                                        else if (format == 3)
                                        {
                                            <input type="checkbox"
                                                   name="selectedCourses[]"
                                                   value="@course.CourseID"
                                                   @(Html.Raw(course.Assigned ? "checked=\"checked\"" : "")) />
                                            @course.CourseID @:  @course.Title
                                        }
                                        cnt++;
                                    @:</td>
                                }
                            @:</tr>
                            }
                        </table>
                    </div>
                </div>
                <div class="form-group">
                    <input type="submit" value="Create" class="btn btn-primary" />
                </div>
            </form>
        </div>
    </div>

    <div>
        <a asp-page="Index">Back to List</a>
    </div>

    @section Scripts {
        @{await Html.RenderPartialAsync("_ValidationScriptsPartial");}
    }
